package com.yootk.action;

import com.yootk.action.abs.AbstractAction;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/pages/cookie/*")
public class CookieAction extends AbstractAction {
    @GetMapping("get")
    @ResponseBody
    public Object get(
            @CookieValue(name = "yootk",defaultValue = "sun.java") String yootk,
            @CookieValue(name="xian",defaultValue = "sunjava") String xian,
            @CookieValue(name = "JSESSIONID") String id
    ){
        Map<String,String> map = new HashMap<>() ;
        map.put("yootk",yootk) ;
        map.put("xian",xian) ;
        map.put("JSESSIONID", id);
        return map ;
    }
    @GetMapping("set")
    @ResponseBody
    public Object set(HttpServletResponse response){
        Cookie cookie1 = new Cookie("yootk","www.yootk.com") ;
        Cookie cookie2 = new Cookie("xian","www.sun.java.com") ;
        cookie1.setPath("/");
        cookie2.setPath("/");
        cookie1.setMaxAge(3600);
        cookie2.setMaxAge(3600);
        response.addCookie(cookie1);
        response.addCookie(cookie2);
        return " set cookie sucess" ;
    }
    @GetMapping("gets")
    @ResponseBody
    public Object gets(
            @CookieValue(name = "yootk", defaultValue = "yootk.com") String yootk,
            @CookieValue(name = "jixianit", defaultValue = "jixianit.com") String jixianit,
            @CookieValue(name = "JSESSIONID", defaultValue = "YOOTK-DEFAULT") String id) {
        Map<String, String> map = new HashMap<>(); // 保存数据的接收结果
        map.put("【Cookie】yootk", yootk);
        map.put("【Cookie】jixianit", yootk);
        map.put("【Cookie】JSESSIONID", id);
        return map;
    }
}
